package com.happiness.base.utils;

import cn.hutool.core.util.StrUtil;
import com.happiness.base.enums.ResultCodeEnum;
import com.happiness.base.utils.exception.AssertionException;

import java.util.Objects;

/**
 * 系统是否断言判断
 * */
public class AssertionUtils {

    /**
     * 是否是null值，不为null抛出异常
     * */
    public static void isNull(Object object){
    isNull(object,ResultCodeEnum.INTERNAL_ERROR.code, "System Error: The specified object is not null");
    }
    /**
     * 是否是null值，不为null抛出异常
     * */
    public static void isNull(Object object,ResultCodeEnum resultCodeEnum){
        isNull(object,resultCodeEnum.code,
                resultCodeEnum.message);
    }

    /**
     * 是否是null值，不为null抛出异常
     * */
    public static void isNull(Object object,Integer code,String message){
        isTrue(!Objects.isNull(object),code, message);

    }

    /**
     * 是否是非null值，为null抛出异常
     * */
    public static void notNull (Object object){
        if (Objects.isNull(object)){
            AssertionException.systemException(ResultCodeEnum.INTERNAL_ERROR.code, "System Error: The specified object is null");
        }
    }

    /**
     * 是否True  不为True则抛出异常
     * */
    public static void isTrue(boolean flag){
        isTrue(flag,ResultCodeEnum.INTERNAL_ERROR.code,
                "System Error: Judge no");
    }

    /**
     * 是否True  不为True则抛出异常
     * */
    public static void isTrue(boolean flag,ResultCodeEnum resultCodeEnum){
            isTrue(flag,resultCodeEnum.code,
                    resultCodeEnum.message);
    }

    /**
     * 是否True  不为True则抛出异常
     * */
    public static void isTrue(boolean flag,Integer code,String message){
        if (!flag){
            AssertionException.systemException(code,message);
        }
    }

    /**
     * 字符是否非空，为空则抛出异常
     * */
    public static void notEmpty(String str){
        if (StrUtil.hasEmpty(str)){
            AssertionException.systemException(ResultCodeEnum.INTERNAL_ERROR.code,
                    "System Error: The character is empty");
        }
    }

    /**
     * 字符是否是空白字符（不可见字符也算空白字符），是则抛出异常
     * */
    public static void notBlank(String str){
        if (StrUtil.hasBlank(str)){
            AssertionException.systemException(ResultCodeEnum.INTERNAL_ERROR.code,
                    "System Error: The character is blank");
        }
    }

}
